package com.gopay.bis.manager.bank.ebank;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.domain.bank.hxbc.ebank.geturl.res.HXEB2CResGetUrl;
import com.gopay.common.domain.bank.hxbc.ebank.pay.req.BodyB2CReqPay;
import com.gopay.common.domain.bank.hxbc.ebank.pay.req.HXEB2CReqPay;
import com.gopay.common.domain.bank.hxbc.ebank.pay.res.BackVerifyB2CResPay;
import com.gopay.common.domain.bank.hxbc.ebank.pay.res.ForeVerifyB2CResPay;
import com.gopay.common.domain.bank.hxbc.ebank.query.req.BodyB2CReqQuery;
import com.gopay.common.domain.bank.hxbc.ebank.query.req.HXEB2CReqQuery;
import com.gopay.common.domain.bank.hxbc.ebank.query.res.HXEB2CResQuery;
import com.gopay.common.domain.bank.hxbc.ebank.req.HeadB2CReq;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.businesses.BankService;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * 华夏支付网关B2C对象封装
 * 
 * @author HxbcB2cManager.java
 * @author LiLiLi
 */
public class HxbcB2cManager {

	@Resource(name = "hxbcConfig")
	private Map<String, String> hxbcConfig;

	@Resource(name = "hxbcB2cBankServiceClient")
	private BankService hxbcB2cBankServiceClient;

	@Resource(name = "hxbcB2cBankServiceClientHna")
	private BankService hxbcB2cBankServiceClientHna;

	/**
	 * 构建B2C签名对象
	 * 
	 * @param HXEB2CReqPay
	 * @return HXEB2CResGetUrl
	 */
	public HXEB2CResGetUrl buildSign(HXEB2CReqPay message)
			throws GopayException {
		message = buildB2C(message);
		String plainStr = this.getPayPlainStr(message);
		message.setPlainMessage(plainStr);
		BankLogUtils.printB2CManagerSignSendLog(message);
		HXEB2CResGetUrl messageGetUrl;
		if (message.isHna()) {
			messageGetUrl = (HXEB2CResGetUrl) hxbcB2cBankServiceClientHna
					.sign(message);
		} else {
			messageGetUrl = (HXEB2CResGetUrl) hxbcB2cBankServiceClient
					.sign(message);
		}
		BankLogUtils.printB2CManagerSignReturnLog(messageGetUrl);
		return messageGetUrl;
	}

	/**
	 * 后台B2C验签
	 * 
	 * @param HXEB2CReqPay
	 * @return BackVerifyB2CResPay
	 */
	public BackVerifyB2CResPay backVerify(HXEB2CReqPay message)
			throws GopayException {
		BackVerifyB2CResPay resPay = new BackVerifyB2CResPay();
		HeadB2CReq reqHead = new HeadB2CReq();
		message.setHead(reqHead);
		BankLogUtils.printB2CManagerVerifySendLog(message);
		if (message.isHna()) {
			resPay = (BackVerifyB2CResPay) hxbcB2cBankServiceClientHna
					.verify(message);
		} else {
			resPay = (BackVerifyB2CResPay) hxbcB2cBankServiceClient
					.verify(message);
		}
		BankLogUtils.printB2CManagerVerifyReturnLog(message, resPay);
		return resPay;

	}

	/**
	 * 前台B2C验签
	 * 
	 * @param HXEB2CReqPay
	 * @return ForeVerifyB2CResPay
	 */
	public ForeVerifyB2CResPay foreVerify(HXEB2CReqPay message)
			throws GopayException {
		BankLogUtils.printB2CManagerVerifySendLog(message);
		ForeVerifyB2CResPay resPay = new ForeVerifyB2CResPay();
		if (message.isHna()) {
			resPay = (ForeVerifyB2CResPay) hxbcB2cBankServiceClientHna
					.verify(message);
		} else {
			resPay = (ForeVerifyB2CResPay) hxbcB2cBankServiceClient
					.verify(message);
		}
		BankLogUtils.printB2CManagerVerifyReturnLog(message, resPay);
		return resPay;
	}

	/**
	 * B2C订单查询
	 * 
	 * @param HXEB2CReqQuery
	 * @return HXEB2CResQuery
	 */
	public HXEB2CResQuery query(HXEB2CReqQuery message) throws GopayException {
		HXEB2CResQuery resQuery = new HXEB2CResQuery();
		message = buildQueryB2C(message);
		String plainStr = this.getQueryPlainStr(message);
		message.setPlainMessage(plainStr);
		BankLogUtils.printB2CManagerQuerySendLog(message, "");
		if (message.isHna()) {
			resQuery = (HXEB2CResQuery) hxbcB2cBankServiceClientHna
					.query(message);
		} else {
			resQuery = (HXEB2CResQuery) hxbcB2cBankServiceClient.query(message);
		}
		BankLogUtils.printB2CManagerQueryReturnLog(resQuery);
		return resQuery;
	}

	/**
	 * 设置银行B2C属性
	 * 
	 * @param HXEB2CReqPay
	 * @return HXEB2CReqPay
	 */
	public HXEB2CReqPay buildB2C(HXEB2CReqPay message) {
		HeadB2CReq reqHead = new HeadB2CReq();
		BodyB2CReqPay reqBody = new BodyB2CReqPay();
		reqHead.setIdentification(hxbcConfig.get("HXBC.B2C.IDENTIFICATION"));
		reqHead.setTrnxCode(hxbcConfig.get("HXBC.B2C.PAY.TRNXCODE"));
		reqHead.setTrnxDatetime(this.getCurrDate());
		reqBody.setCustType(hxbcConfig.get("HXBC.B2C.CUSTYPE"));
		// reqBody.setMerchantId(message.getMerID());
		reqBody.setMerchantId(hxbcConfig.get("HXBC.B2C.MERCHANTID"));
		reqBody.setOrderNo(message.getBody().getOrderNo());
		reqBody.setOrderAmt(message.getBody().getOrderAmt());
		reqBody.setCurrency(hxbcConfig.get("HXBC.B2C.CURRTYPE"));
		reqBody.setTrnxType(hxbcConfig.get("HXBC.B2C.TRNXTYPE"));
		/**
		 * 配置前台通知地址
		 */
		reqBody.setRemark2(message.getPayNotifyUrl());
		//reqBody.setRemark2("https://bisgray.gopay.com.cn/HxbcB2cFrontRes.shtml");
		message.setHead(reqHead);
		message.setBody(reqBody);
		return message;
	}

	/**
	 * 设置银行查询B2C属性
	 * 
	 * @param HXEB2CReqPay
	 * @return HXEB2CReqPay
	 */
	public HXEB2CReqQuery buildQueryB2C(HXEB2CReqQuery message) {
		HeadB2CReq reqHead = new HeadB2CReq();
		BodyB2CReqQuery reqBody = new BodyB2CReqQuery();
		reqHead.setIdentification(hxbcConfig.get("HXBC.B2C.IDENTIFICATION"));
		reqHead.setTrnxCode(hxbcConfig.get("HXBC.B2C.QUERY.TRNXCODE"));
		reqHead.setTrnxDatetime(this.getCurrDate());
		// reqBody.setMerchantId(message.getMerID());
		reqBody.setMerchantId(hxbcConfig.get("HXBC.B2C.MERCHANTID"));
		reqBody.setSearchType(hxbcConfig.get("HXBC.B2C.QUERY.SEARCHTYPE"));
		reqBody.setTrnxType(hxbcConfig.get("HXBC.B2C.TRNXTYPE"));
		reqBody.setOrderNo(message.getOrderId());
		message.setHead(reqHead);
		message.setBody(reqBody);
		return message;
	}

	/**
	 * 构建支付明文
	 * 
	 * @param HXEB2CReqPay
	 * @return String
	 */
	public String getPayPlainStr(HXEB2CReqPay message) {
		XStream xstream = new XStream(new DomDriver());
		xstream.autodetectAnnotations(true);
		String plainStr = xstream.toXML(message);
		return plainStr;
	}

	/**
	 * 构建订单查询明文
	 * 
	 * @param HXEB2CReqQuery
	 * @return String
	 */
	public String getQueryPlainStr(HXEB2CReqQuery message) {
		XStream xstream = new XStream(new DomDriver());
		xstream.autodetectAnnotations(true);
		String plainStr = xstream.toXML(message);
		return plainStr;
	}

	/**
	 * 获取当前时间
	 * 
	 * @return String
	 */
	private String getCurrDate() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String s = sdf.format(date);
		return s;
	}
}
